package com.quyunshuo.module.home.fragment.project

import android.util.Log
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.quyunshuo.androidbaseframemvvm.base.ktx.observeLiveData
import com.quyunshuo.androidbaseframemvvm.common.ui.BaseFragment
import com.quyunshuo.androidbaseframemvvm.common.utils.ToastUtil
import com.quyunshuo.androidbaseframemvvm.network.bean.ArticleBean
import com.quyunshuo.androidbaseframemvvm.network.bean.ArticleListRes2
import com.quyunshuo.module.home.databinding.HomeFragmentProjectBinding
import com.quyunshuo.module.home.fragment.CustomText
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author DBoy 2021/7/6 <p>
 * - 文件描述 : 测试fragment
 */
@AndroidEntryPoint
class ProjectFragment : BaseFragment<HomeFragmentProjectBinding, ProjectViewModel>() ,
    OnRefreshLoadMoreListener{
    private val TAG = "InternalFragment"
    override val mViewModel by viewModels<ProjectViewModel>()

    override fun createVB() = HomeFragmentProjectBinding.inflate(layoutInflater)

    private lateinit var adapter: ProjectAdapter
    private val dataList = mutableListOf<ArticleBean>()
    private var page = 1
    private var isRefresh = true;//true是刷新 false是累加

    override fun HomeFragmentProjectBinding.initView() {
        //瀑布流的效果~通过设置view的高度不同来完成。
        recyclerView.layoutManager = StaggeredGridLayoutManager(
            2,  // 列数
            StaggeredGridLayoutManager.VERTICAL
        )
        recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                Log.d(TAG, "onScrolled: recyclerview滚动中")
            }
        })
        adapter = ProjectAdapter(dataList)
        recyclerView.adapter = adapter
        smartRefreshLayout.setOnRefreshLoadMoreListener(this@ProjectFragment)
        loadData()

    }


    private fun loadData() {
        mViewModel.getData(page)
    }

    override fun initObserve() {
        observeLiveData(mViewModel.firstData,::videoDome)
    }

    private fun videoDome(articleListRes2: ArticleListRes2?) {
        val datas = articleListRes2?.getDatas()
        if (isRefresh) {
            dataList.clear()
            if (datas!!.isEmpty()){
                ToastUtil.showToast("数据为空")
            }
            // 添加模拟数据
            articleListRes2?.getDatas()!!.forEach{dataList.add(it)}
            mBinding.smartRefreshLayout.finishRefresh()
        } else {
            if (datas!!.isEmpty()){
                ToastUtil.showToast("数据已加载完毕")
            }
            articleListRes2?.getDatas()!!.forEach{dataList.add(it)}
            mBinding.smartRefreshLayout.finishLoadMore()

        }
        adapter.notifyDataSetChanged()

    }

    override fun initRequestData() {
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        page = 1
        isRefresh = true
        loadData()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        page++
        isRefresh = false
        loadData()
    }

    override fun onDestroyView() {
        mBinding.recyclerView.adapter = null
        super.onDestroyView()
    }
}